package com.xinhe.web.service;

import com.xinhe.web.entity.AssetDetail;
import com.xinhe.web.reposity.AssetCountRepository;
import com.xinhe.web.reposity.AssetDetailRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Service

public class AssetDetailService {

    /*@Autowired
    private IUserSessionManager userSessionManager;*/
    @Autowired
    private AssetDetailRepository assetDetailRepository;
    @Autowired
    private AssetCountRepository assetCountRepository;
    /*@Autowired
    private IWeixinJsapiTradeService weixinJsapiPayService;*/

    @Transactional
    public AssetDetail saveAssetDetail(Long userId,String assetType,String tradeType,
                                       BigDecimal tradeAmount,Long fBusinessId,
                                       String remark,Date createTime,BigDecimal remainder) {
        AssetDetail assetDetail = new AssetDetail();
        assetDetail.setAssetType(assetType);
        if (!Objects.isNull(fBusinessId)){
            assetDetail.setfBusinessId(fBusinessId);
        }
        assetDetail.setRemainder(remainder);
        //assetDetail.setRemark(remark);
        assetDetail.setTradeAmount(tradeAmount);
        assetDetail.setTradeType(tradeType);
        assetDetail.setfUserId(userId);
        assetDetail.setCreateTime(createTime);
        this.assetDetailRepository.save(assetDetail);
        return assetDetail;
    }

    /*@Transactional(rollbackFor = {Exception.class})
    public BeanResult<WeixinJsapiPayParamView> requestWeixinJsapiRechargeParam(AssetRechargeModel model) {
        Long userId = this.userSessionManager.getUserId();
        AssetDetail assetDetail = new AssetDetail();
        assetDetail.setAssetType(model.getAssetType());
        assetDetail.setRemark(model.getRemark());
        assetDetail.setRemoved(Integer.valueOf(-1));
        assetDetail.setTradeAmount(model.getTradeAmount());
        assetDetail.setTradeType(model.getTradeType());
        assetDetail.setUserId(userId);
        this.assetDetailDao.save((BaseEntityImpl)assetDetail);

        if (model.getNotifyUrl() == null) {
            model.setNotifyUrl("/assetDetail/responseWeixinJsapiRechargeNotify");
        }
        WeixinJsapiPayParamModel weixinModel = new WeixinJsapiPayParamModel();
        weixinModel.setDataId(assetDetail.getId());
        weixinModel.setBody("充值");
        weixinModel.setFee(model.getTradeAmount());
        weixinModel.setNotifyUrl(model.getNotifyUrl());
        return this.weixinJsapiPayService.requestWeixinPayParam((WeixinPayParamModel)weixinModel);
    }


    @Transactional(rollbackFor = {Exception.class})
    public BeanResult<String> responseWeixinJsapiRechargeNotify() {
        BeanResult<String> result = new BeanResult();

        BeanResult<WeixinPayNotifyView> notifyResult = this.weixinJsapiPayService.getWeixinPayNotifyDataAndValidateAndUpdateWeixinTrade();
        WeixinPayNotifyView notify = (WeixinPayNotifyView)notifyResult.getItem();
        WeixinTrade trade = notify.getWeixinTrade();
        AssetDetail assetDetail = (AssetDetail)this.assetDetailDao.findById(trade.getDataId());
        AssetCount assetCount = this.assetCountDao.findOneByTypeAndUserId(assetDetail
                .getAssetType(), assetDetail.getUserId());
        if (null == assetCount) {
            assetCount = new AssetCount();
            assetCount.setUserId(assetDetail.getUserId());
            assetCount.setType(assetDetail.getAssetType());
            this.assetCountDao.save((BaseEntityImpl)assetCount);
        }
        BigDecimal remainder = assetCount.getRemainder().add(assetDetail.getTradeAmount());
        assetCount.setRemainder(remainder);
        this.assetCountDao.update((BaseEntityImpl)assetCount);
        assetDetail.setRemainder(remainder);
        assetDetail.setRemoved(Integer.valueOf(0));
        this.assetDetailDao.update((BaseEntityImpl)assetDetail);
        result.setItem(notifyResult.getMsg());
        return result;
    }

    public PageResult<AssetDetailView> pageAssetDetail(AssetDetailQuery query) {
        Integer total = Integer.valueOf(1);
        if (null == query.getUserId()) {
            query.setUserId(this.userSessionManager.getUserId());
        }
        if (0 < query.getLimit().intValue()) {
            total = this.assetDetailDao.countAssetDetail(query);
        }
        List<AssetDetailView> views = null;
        if (0 < total.intValue()) {
            views = this.assetDetailDao.pageAssetDetail(query);
        }
        if (0 > query.getLimit().intValue()) {
            if (null == views) {
                total = Integer.valueOf(0);
            } else {
                total = Integer.valueOf(views.size());
            }
        }
        return new PageResult(total, views);
    }*/

}
